package org.ielts.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.AppointConsultorLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import com.yifeng.dao.HibernateRepository;
import com.yifeng.page.table.Paginations;

@Repository
public class SignCourseLogRepository extends HibernateRepository<SignCourseLog, String> {

	public long countByConsultant(String id) {
		DetachedCriteria criteria = createDetachedCriteria();

		criteria.createCriteria("consultant").add(Restrictions.eq("id", id));

		long total = countRowByCriteria(criteria);

		return total;
	}

	public List<SignCourseLog> search(String name, Boolean checked, String[] constansId, Pageable pageable) {
		DetachedCriteria criteria = createDetachedCriteria();

		if (name != null && name.trim().length() > 0) {
			criteria.createAlias("student", "s").add(
					Restrictions.or(Restrictions.eq("s.name", name), Restrictions.eq("s.pinyin", name)));
		}
		if (checked != null) {
			criteria.add(Restrictions.eq("checked", checked));
		}

		if (constansId != null && constansId.length > 0) {
			criteria.createAlias("consultant", "c").add(Restrictions.in("c.id", constansId));
		}

		criteria.addOrder(Order.desc("createTime"));

		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

		@SuppressWarnings("unchecked")
		List<SignCourseLog> list = hibernateTemplate.findByCriteria(criteria, pageable.getOffset(),
				pageable.getPageSize());

		return list;
	}

	public long countSearch(String name, Boolean checked, String[] constansId) {
		DetachedCriteria criteria = createDetachedCriteria();

		if (name != null && name.trim().length() > 0) {
			criteria.createAlias("student", "s").add(
					Restrictions.or(Restrictions.eq("s.name", name), Restrictions.eq("s.pinyin", name)));
		}
		if (checked != null) {
			criteria.add(Restrictions.eq("checked", checked));
		}
		if (constansId != null && constansId.length > 0) {
			criteria.createAlias("consultant", "c").add(Restrictions.in("c.id", constansId));
		}

		long total = countRowByCriteria(criteria);

		return total;
	}

}
